﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyKhachHang_SuaThongTinKhachHang : System.Web.UI.Page
{
    private string strMa = string.Empty;
    private DataAccess da = new DataAccess();
    private Entities en = new Entities();
    private string mk = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (Request.QueryString["maKH"] != null && Request.QueryString["maKH"].ToString() != "")
        {
            strMa = Request.QueryString["maKH"].ToString();
        }
        if (!IsPostBack)
        {
            this.GetKhachHanginfo(strMa);
        }
    }

    private void GetKhachHanginfo(string ma)
    {
        SqlDataReader drd;
        SqlCommand comm = new SqlCommand("getKhachHangbyMa", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@Ma",ma);
        comm.Connection.Open();
        drd = comm.ExecuteReader();
        if (drd.Read())
        {
            this.txtMaKH.Text = drd["MaKH"].ToString();
            this.txtTenKH.Text = drd["HoTenKH"].ToString();
            this.txtdiachi.Text = drd["DiaChiKH"].ToString();
            this.txtPhoneKH.Text = drd["PhoneKH"].ToString();
            this.txttentruycap.Text = drd["TenTruyCapKH"].ToString();
             mk = drd["MatKhauKH"].ToString();
		
        }
        drd.Close();
        drd.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();
    }
    protected void NhomHangHoa_Edit_Click(object sender, EventArgs e)
    {
        EditKhachHang();
    }

    private void EditKhachHang()
    {

        string strTen = this.txtTenKH.Text;
        string strDiaChi = this.txtdiachi.Text;
        string strPhone = this.txtPhoneKH.Text;
        string strU = this.txttentruycap.Text;
        string strP = this.txtnewpass.Text;
        string sP = "";
        if (strP.Equals(""))
        {
            sP = mk;
        }
        else {
            sP = strP;
        }
        SqlCommand comm = new SqlCommand("updateKH", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MaKH", strMa);
        comm.Parameters.AddWithValue("@TenKH", strTen);
        comm.Parameters.AddWithValue("@DiaChi", strDiaChi);
        comm.Parameters.AddWithValue("@Phone", strPhone);
        comm.Parameters.AddWithValue("@user", strU);
        comm.Parameters.AddWithValue("@pass",en.EncodePassword(sP,""));

        comm.Connection.Open();
        comm.ExecuteNonQuery();




        comm.Connection.Close();
        comm.Connection.Dispose();
        Response.Redirect("DanhSachKhachHang.aspx");

    }
}
